﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using LevelEditor.App;
using LevelEditor.Components.Layers;

namespace LevelEditor
{
    public partial class NewLayer : Form
    {
        public enum LayerResult
        {
            Canceled, Success,
        }

        public enum State
        {
            NewLayer, EditLayer,
        }

        private LevelEditor editor;
        private DialogResult result;

        private bool wantToClose;

        private LayerItem itemToalter;

        private State state;

        public NewLayer(LevelEditor editor)
        {
            InitializeComponent();
            this.editor = editor;
            this.FormClosing += NewLayer_FormClosing;
            this.wantToClose = false;

            this.state = State.NewLayer;
        }

        public NewLayer(LevelEditor editor, LayerItem item)
        {
            InitializeComponent();

            this.itemToalter = item;
            this.editor = editor;
            this.FormClosing += NewLayer_FormClosing;
            this.wantToClose = false;

            this.state = State.EditLayer;

            InitializeLayerInfo();
        }

        private void InitializeLayerInfo()
        {
            layerName.Text = itemToalter.LayerName.Text;
            tileWidth.Text = itemToalter.TileWidth.ToString();
            tileHeight.Text = itemToalter.TileHeight.ToString();
            rows.Text = itemToalter.Rows.ToString();
            cols.Text = itemToalter.Cols.ToString();
        }
        
        void NewLayer_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (e.CloseReason == CloseReason.UserClosing && !wantToClose)
            {
                result = DialogResult.Cancel;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (!CheckData()) return;

            this.result = DialogResult.OK;

            this.wantToClose = true;
            this.Close();
        }

        private bool CheckData()
        {
            layerName.Text = layerName.Text.Trim();
            tileWidth.Text = tileWidth.Text.Trim();
            tileHeight.Text = tileHeight.Text.Trim();
            rows.Text = rows.Text.Trim();
            cols.Text = cols.Text.Trim();

            if (layerName.Text == "" || tileHeight.Text == "" || tileWidth.Text == "" || rows.Text == "" || cols.Text == "")
            {
                ErrorController.ShowOkError(ErrorController.LevelEditorError.APP_FILL_INFO);
                return false;
            }

            try
            {
                int k = int.Parse(tileWidth.Text);
                k = int.Parse(tileHeight.Text);
                k = int.Parse(rows.Text);
                k = int.Parse(cols.Text);

            }
            catch (Exception)
            {
                ErrorController.ShowOkError(ErrorController.LevelEditorError.APP_JUST_NUMBERS_PERMITED);
                return false;
            }

            return true;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            result = DialogResult.Cancel;
            this.wantToClose = true;
            this.Close();
        }

        public new DialogResult ShowDialog()
        {
            base.ShowDialog();

            return result;
        }

        public string getLayerName()
        {
            return layerName.Text;
        }

        public int getRows()
        {
            return int.Parse(rows.Text);
        }

        public int getCols()
        {
            return int.Parse(cols.Text);
        }

        public int getTileWidth()
        {
            return int.Parse(tileWidth.Text);
        }

        public int getTileHeight()
        {
            return int.Parse(tileHeight.Text);
        }

    }
}
